www.gusucode.com > VC 编程实现修改DNS地址 > VC 编程实现修改DNS地址/gusucode/dns/dnsDlg.cpp
//Download by http://www.NewXing.com // dnsDlg.cpp : implementation file // 会用到系统 windows\system32\dhcpcsvc.dll文件; #include "stdafx.h" #include <string> #include <vector> #include <winsock2.h> #include "ini.h" #include "dns.h" #include "dnsDlg.h" #pragma comment(lib,"ws2_32.lib") CString CheckIP(); //全局变量 CString GetDNS; BOOL ModifyDNS; CString DNSValue; CString AdapterName; BOOL winHide; CString iniDNS; BOOL autorunDia; #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif using namespace std; typedef struct tagAdapterInfo { string strName; // 适配器名称 string strDriverDesc; // 适配器描述 string strIP; // IP地址 string strNetMask; // 子网掩码 string strNetGate; // 网关 }ADAPTER_INFO; BOOL GetAdapterInfo(); BOOL RegGetIP(ADAPTER_INFO *pAI, LPCTSTR lpszAdapterName, int nIndex = 0); typedef int (CALLBACK* DHCPNOTIFYPROC)(LPWSTR, LPWSTR, BOOL, DWORD, DWORD, DWORD, int); //BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate, LPCTSTR pNetDNS); BOOL RegSetIP(LPCTSTR lpszAdapterName, LPCTSTR pNetDNS); vector<ADAPTER_INFO*> AdapterInfoVector; ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDnsDlg dialog CDnsDlg::CDnsDlg(CWnd* pParent /*=NULL*/) : CDialog(CDnsDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDnsDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDnsDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDnsDlg) DDX_Control(pDX, IDC_CHECK2, m_checkAutoRunDia); DDX_Control(pDX, IDC_CHECK1, m_chkAutoRun); DDX_Control(pDX, IDC_EDIT1, m_editDNS); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDnsDlg, CDialog) //{{AFX_MSG_MAP(CDnsDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON2, OnButton2) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_WM_DESTROY() ON_BN_CLICKED(IDC_BUTTON3, OnButton3) ON_BN_CLICKED(IDC_CHECK1, OnCheck1) ON_BN_CLICKED(IDC_BUTTON5, OnButton5) ON_BN_CLICKED(IDC_CHECK2, OnCheck2) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDnsDlg message handlers BOOL CDnsDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here //初始化变量 GetDNS = ""; ModifyDNS = FALSE; DNSValue = ""; AdapterName = ""; winHide = 0; iniDNS = ""; BOOL aurun = 0; autorunDia = 0; // 得到适配器信息 GetAdapterInfo(); // 得到适配器信息 //创建和读取INI文件 //先检查文件是否存在,不存在创建此文件 CStdioFile IniFile; CIni Ini("DNS.ini"); //CIni Ini("DNS.ini"); if (IniFile.Open(".\\DNS.ini",CStdioFile::modeNoTruncate|CStdioFile::modeReadWrite)) ; else if (IniFile.Open(".\\DNS.ini",CStdioFile::modeCreate)) { //初始化配置文件DNS.ini内容 IniFile.Close(); Ini.Write("CONFIG", "AUTORUN", "0"); //Ini.Write("CONFIG", "DNS", (LPSTR)(LPCTSTR)GetDNS); Ini.Write("CONFIG", "OVERDIA", "0"); Ini.Write("CONFIG", "DNS", ""); Ini.Save(); //MessageBox(NULL,"cs_temp.cfg文件创建并打开了",NULL,SW_SHOW); } IniFile.Close(); //读DNS值 iniDNS = Ini.ReadText("CONFIG", "DNS"); //MessageBox(iniDNS); //检测是否自动运行 aurun = Ini.ReadInt("CONFIG", "AUTORUN"); //CString s; //s.Format("%d",aurun); //MessageBox(s); if(aurun) m_chkAutoRun.SetCheck(1); //选中 else m_chkAutoRun.SetCheck(0); //不选中 //检测自动运行是否出提示框 autorunDia = Ini.ReadInt("CONFIG", "OVERDIA"); if(autorunDia) m_checkAutoRunDia.SetCheck(1); //选中 else m_checkAutoRunDia.SetCheck(0); //不选中 Ini.Close(); //CIni Ini("DNS.ini"); //取得当前活动网卡的IP //CheckIP(); // 得到适配器信息 GetAdapterInfo(); // 得到适配器信息 //添加到IP框中 //DWORD dwGate; //dwGate = inet_addr(CheckIP()); //unsigned char *pGate = (unsigned char*)&dwGate; //m_ctlIPAddrDNS1.SetAddress(*pGate, *(pGate+1), *(pGate+2), *(pGate+3)); //添加到Edit m_editDNS.SetWindowText(GetDNS); //添加到list box 内 //m_listDNS.AddString(GetDNS); // 添加到ComboBox /* for( size_t i = 0; i < AdapterInfoVector.size(); i++ ) { MessageBox(AdapterInfoVector[i]->strDriverDesc.c_str()); m_ctlAdapters.AddString(AdapterInfoVector[i]->strDriverDesc.c_str()); } m_ctlAdapters.SetCurSel(0); OnSelchangeAdapter(); */ //自动运行选中后开始修改DNS if(aurun) { //MessageBox("1"); winHide = 1; } else { //MessageBox("ads"); winHide = 0; } return TRUE; // return TRUE unless you set the focus to a control } void CDnsDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CDnsDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } //隐藏窗口运行 if(winHide) { ShowWindow(SW_HIDE); // //MessageBox(iniDNS); RegSetIP(AdapterName, iniDNS); if(autorunDia) { if(::MessageBox(NULL, "修改DNS成功!", "提示:", MB_OK)) PostQuitMessage(0); // 修改成功后退出 } else PostQuitMessage(0); // 修改成功后退出 } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDnsDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } //返回本机当前活动网卡的IP CString CheckIP() { WSADATA wsaData; char name[155]; char *ip = ""; //CString s = ""; PHOSTENT hostinfo; if ( WSAStartup( MAKEWORD(2,0), &wsaData ) == 0 ) { if( gethostname ( name, sizeof(name)) == 0) { if((hostinfo = gethostbyname(name)) != NULL) { //这些就是获得IP的函数 ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list); //MessageBox(NULL, ip, NULL, MB_OK); return ip; //printf("%s\n",ip); } } WSACleanup(); } return ip; } //----------------------------------------------------------------- // 取得所有网卡信息 //----------------------------------------------------------------- BOOL GetAdapterInfo() { // 这里的代码适合WINDOWS2000,对于NT需要读取HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards HKEY hKey, hSubKey, hNdiIntKey; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Class\\{4d36e972-e325-11ce-bfc1-08002be10318}", 0, KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE; DWORD dwIndex = 0; DWORD dwBufSize = 256; DWORD dwDataType; char szSubKey[256]; unsigned char szData[256]; while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) { if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS) { if(RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS) { dwBufSize = 256; if(RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) { if(strcmp((char*)szData, "ethernet") == 0) // 判断是不是以太网卡 { dwBufSize = 256; if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) { //CString s; ADAPTER_INFO *pAI = new ADAPTER_INFO; //s = (LPCTSTR)szData; //MessageBox(NULL, s, "00", MB_OK); pAI->strDriverDesc = (LPCTSTR)szData; dwBufSize = 256; if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) { pAI->strName = (LPCTSTR)szData; //s = (LPCTSTR)szData; //MessageBox(NULL, s, "00", MB_OK); RegGetIP(pAI, (LPCTSTR)szData); } AdapterInfoVector.push_back(pAI); // 加入到容器中 } } } RegCloseKey(hNdiIntKey); } RegCloseKey(hSubKey); } dwBufSize = 256; } /* end of while */ RegCloseKey(hKey); /* // 可以使用GetAdaptersInfo来取得网卡信息,但其显示的名称不是很具体 ULONG ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO); IP_ADAPTER_INFO *pAdapterInfoBkp, *pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize]; if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // 缓冲区不够大 { delete pAdapterInfo; pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize]; pAdapterInfoBkp = pAdapterInfo; } if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS ) { do { if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) { ADAPTER_INFO *pAI = new ADAPTER_INFO; pAI->strDriverDesc = pAdapterInfo->Description; pAI->strName = pAdapterInfo->AdapterName; RegGetIP(pAI, (LPCTSTR)pAdapterInfo->AdapterName); // 因为IP_ADAPTER_INFO中未包含掩码信息,所以干脆直接读注册表 AdapterInfoVector.push_back(pAI); } pAdapterInfo = pAdapterInfo->Next; } while(pAdapterInfo); } delete pAdapterInfoBkp; */ return TRUE; } //----------------------------------------------------------------- // 得到注册表中的IP信息 // nIndex暂时未处理 //----------------------------------------------------------------- BOOL RegGetIP(ADAPTER_INFO *pAI, LPCTSTR lpszAdapterName, int nIndex/* =0 */) { CString s; ASSERT(pAI); HKEY hKey; string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"; strKeyName += lpszAdapterName; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKeyName.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS) return FALSE; unsigned char szData[256]; DWORD dwDataType, dwBufSize; dwBufSize = 256; if(RegQueryValueEx(hKey, "IPAddress", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) pAI->strIP = (LPCTSTR)szData; //s = (LPCTSTR)szData; //MessageBox(NULL, s, "00", MB_OK); if(CheckIP() == (LPCTSTR)szData) { AdapterName = lpszAdapterName; //MessageBox(NULL, AdapterName, "信息:", MB_OK); //if(ModifyDNS == FALSE) //{ //MessageBox(NULL, "找到当前活动网卡IP", "", MB_OK); dwBufSize = 256; if(RegQueryValueEx(hKey, "SubnetMask", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) pAI->strNetMask = (LPCTSTR)szData; //s = (LPCTSTR)szData; //MessageBox(NULL, s, "00", MB_OK); dwBufSize = 256; if(RegQueryValueEx(hKey, "DefaultGateway", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) pAI->strNetGate = (LPCTSTR)szData; //s = (LPCTSTR)szData; //MessageBox(NULL, s, "00", MB_OK); dwBufSize = 256; if(RegQueryValueEx(hKey, "NameServer", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) //pAI->strNetGate = (LPCTSTR)szData; //CDnsDlg::m_editDNS.SetWindowText((LPCTSTR)szData); //CDnsDlg::m_editDNS->SetWindowText((LPCTSTR)szData); GetDNS = (LPCTSTR)szData; //s = (LPCTSTR)szData; //MessageBox(NULL, s, "", MB_OK); //} /* else { //dwBufSize = 256; char *DNSstring; DNSstring = (LPSTR)(LPCTSTR)DNSValue; MessageBox(NULL, DNSstring, "信息:", MB_OK); MessageBox(NULL, (unsigned char*)DNSstring, "信息:", MB_OK); //RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate); if(RegSetValueEx(hKey, "NameServer", 0, REG_MULTI_SZ, (unsigned char*)DNSstring, sizeof(DNSstring)) == ERROR_SUCCESS) //m_editDNS.SetWindowText(GetDNS); MessageBox(NULL, "修改DNS成功!", "信息:", MB_OK); }*/ } RegCloseKey(hKey); return TRUE; } /* void GetNetInfo() { //int nSel = m_ctlAdapters.GetCurSel(); DWORD dwIP, dwMask, dwGate; //dwIP = inet_addr(AdapterInfoVector[nSel]->strIP.c_str()); //dwMask = inet_addr(AdapterInfoVector[nSel]->strNetMask.c_str()); dwGate = inet_addr(AdapterInfoVector[nSel]->strNetGate.c_str()); //unsigned char *pIP = (unsigned char*)&dwIP; //unsigned char *pMask = (unsigned char*)&dwMask; unsigned char *pGate = (unsigned char*)&dwGate; //m_ctlIPAddress.SetAddress(*pIP, *(pIP+1), *(pIP+2), *(pIP+3)); //m_ctlNetMask.SetAddress(*pMask, *(pMask+1), *(pMask+2), *(pMask+3)); m_ctlNetGate.SetAddress(*pGate, *(pGate+1), *(pGate+2), *(pGate+3)); } */ void CDnsDlg::OnButton2() //刷新按钮 { // TODO: Add your control notification handler code here GetAdapterInfo(); // 得到适配器信息 //添加到Edit m_editDNS.SetWindowText(GetDNS); } void CDnsDlg::OnButton1() //修改按钮 { //只需要修改注册表 //MessageBox(AdapterName); m_editDNS.GetWindowText(DNSValue); //ModifyDNS = TRUE; //GetAdapterInfo(); //ModifyDNS = FALSE; //OnButton2(); //MessageBox(DNSValue); //MessageBox(AdapterName); RegSetIP(AdapterName, DNSValue); OnButton2(); } void CDnsDlg::OnDestroy() { // TODO: Add your message handler code here // 做最后的清理 for( size_t i = 0; i < AdapterInfoVector.size(); i++ ) delete AdapterInfoVector[i]; AdapterInfoVector.clear(); CDialog::OnDestroy(); } //----------------------------------------------------------------- // 设置注册表中的IP信息 //----------------------------------------------------------------- //BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate, LPCTSTR pNetDNS) BOOL RegSetIP(LPCTSTR lpszAdapterName, LPCTSTR pNetDNS) { HKEY hKey; string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\"; strKeyName += lpszAdapterName; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, strKeyName.c_str(), 0, KEY_WRITE, &hKey) != ERROR_SUCCESS) return FALSE; //char mszIPAddress[100]; //char mszNetMask[100]; //char mszNetGate[100]; char mszNetDNS[100]; //strncpy(mszIPAddress, pIPAddress, 98); //strncpy(mszNetMask, pNetMask, 98); //strncpy(mszNetGate, pNetGate, 98); strncpy(mszNetDNS, pNetDNS, 98); //int nIP, nMask, nGate; int nDNS; //nIP = strlen(mszIPAddress); //nMask = strlen(mszNetMask); //nGate = strlen(mszNetGate); nDNS = strlen(mszNetDNS); //*(mszIPAddress + nIP + 1) = 0x00; //nIP += 2; //*(mszNetMask + nMask + 1) = 0x00; //nMask += 2; //*(mszNetGate + nGate + 1) = 0x00; //nGate += 2; //*(mszNetDNS + nDNS + 1) = 0x00; //nDNS += 2; //RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP); //RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask); //RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate); //MessageBox(NULL, "ASDAS", "信息:", MB_OK); //NameServer 的类型是REG_SZ if(RegSetValueEx(hKey, "NameServer", 0, REG_SZ, (unsigned char*)mszNetDNS, nDNS) == ERROR_SUCCESS) if (!winHide) { MessageBox(NULL, "修改DNS成功!", "信息:", MB_OK); } else if (!winHide) { MessageBox(NULL, "修改DNS没有成功!", "信息:", MB_OK); } RegCloseKey(hKey); return TRUE; } void CDnsDlg::OnButton3() { // TODO: Add your control notification handler code here CAboutDlg().DoModal(); } void CDnsDlg::OnCheck1() //设置是否自动运行 { // TODO: Add your control notification handler code here CIni Ini("DNS.ini"); if(m_chkAutoRun.GetCheck() == 1) //选中 { Ini.Write("CONFIG", "AUTORUN", "1"); } else Ini.Write("CONFIG", "AUTORUN", "0"); Ini.Save(); Ini.Close(); } void CDnsDlg::OnButton5() //打开配置文件 { // TODO: Add your control notification handler code here ShellExecute(NULL,NULL,_T(".\\DNS.ini"),NULL,NULL,SW_NORMAL); } void CDnsDlg::OnCheck2() //自动运行后出提示框 { // TODO: Add your control notification handler code here CIni Ini("DNS.ini"); if(m_checkAutoRunDia.GetCheck() == 1) //选中 { Ini.Write("CONFIG", "OVERDIA", "1"); } else Ini.Write("CONFIG", "OVERDIA", "0"); Ini.Save(); Ini.Close(); }